<html>
<header>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
        body {
            margin: 0px;
        }

        .timer {
            position: absolute;
            height: 32px;
            line-height: 32px;
            /* top: 5px; */
            left: 60%;
            margin-left: 150px;
            font-size: 12px;
            color: #fff;
            display: flex;
        }

        #timer {
            display: none;
        }

        .a {
            font-size: 12px;
            margin-left: 30px;
            text-decoration: none;
            color: #fff;
        }
    </style>
</header>

<body onunload="goodbye()">
    <div class="form">
        <div class="timer">
            <div align="center" id="countdown">
                <span>00</span>:<span>00</span>
                <input id='timer' type="button" value="开始" />
                <a class="a" href="" onclick="close_window()">退出自学</a>
            </div>

        </div>
        <div id="iframeEditor">
        </div>
    </div>
</body>

</html>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
    var TimeSum = ''
    var conf = ''
    var newurl = ''
    newurl = getUrlParam('urldata')
    console.log(newurl, 'new');
    window.onload = function () {

        var oCountDown = document.getElementById("countdown");
        var aInput = oCountDown.getElementsByTagName("input")[0];
        var timer = null;
        aInput.onclick = function () {
            this.className == "" ? (timer = setInterval(updateTime, 1000), updateTime()) : (clearInterval(timer));
            this.className = this.className == '' ? "cancel" : '';
        };
        function format(a) {
            return a.toString().replace(/^(\d)$/, '0$1');
        }
        function updateTime() {
            var aSpan = oCountDown.getElementsByTagName("span");
            var oRemain = aSpan[0].innerHTML.replace(/^0/, '') * 60 + parseInt(aSpan[1].innerHTML.replace(/^0/, ''));
            // if (oRemain <= 0) {
            //     clearInterval(timer);
            //     return;
            // }
            oRemain++;
            aSpan[0].innerHTML = format(parseInt(oRemain / 60));
            oRemain %= 60;
            aSpan[1].innerHTML = format(parseInt(oRemain));
            var S = aSpan[1].innerHTML
            var M = aSpan[0].innerHTML
            TimeSum = Number(M * 60) + Number(S)
            // console.log(TimeSum,'oRemain');
        }
        // 加上此句即可。
        aInput.click();
    }

    function close_window() {
        var lessonType = Number(getUrlParam('type'))  || 1
        var lessonName = conf.document.title
        var lessonId = getUrlParam('documentId')
        var token = getUrlParam('token')
        var data = { lessonType: lessonType, lessonName: lessonName, lessonId: lessonId, learnDuration: TimeSum }
        var confirms = confirm("你查看的网页正在试图关闭窗口。是否关闭此窗口")
        if (confirms) {
            $.ajax({
                url: newurl + '/api/ts/learn_record/save',
                type: 'post',
                data: JSON.stringify(data),
                cache: false,
                async: false,
                headers: {
                    "Authorization": token,
                    'Content-Type': 'application/json'  //multipart/form-data;boundary=--xxxxxxx   application/json
                },
                success: function (data) {
                    console.log(data, '64646464646');
                    window.close("OpenWindow.html", "_self");//火狐浏览器不支持关闭
                }
            })

        }
        else {
            alias('wwwwwwwwww')
            //不执行任何操作
        }
    }




    $(function () {
        var url = newurl + '/api/fd/document/getDocument'
        var documentId = getUrlParam('documentId')
        var mode = getUrlParam('mode')
        var data
        if (mode && mode == 'edit') {
            data = {
                documentId: documentId,
                mode: "edit"
            }
        } else {
            data = {
                documentId: documentId,
                mode: "view"
            }
        }
        requestData(url, data, res => {
            let scriputDom = document.createElement("script");
            scriputDom.onload = () => {
                conf = JSON.parse(res.data.config)
                config = Object.assign({}, config, conf)
                сonnectEditor();
            }
            scriputDom.src = res.data.api;
            document.body.appendChild(scriputDom);
        })
    })
    function requestData(url, data, success, error) {
        var token = getUrlParam('token')
        $.ajax({
            url: url,
            type: 'post',
            dataType: 'json',
            data: JSON.stringify(data),
            cache: false,
            headers: {
                "Authorization": token,
                'Content-Type': 'application/json'  //multipart/form-data;boundary=--xxxxxxx   application/json
            },
            success,
            error
        })
    }
    /**
     * 输入文档名称，如果为空，重复弹窗
     * @param name 参数名称
     * @returns {string| 参数值}
     */
    function inpDocumentName() {
        var code = prompt("请输入文档名称");
        if (code) {
            return code
        } else {
            alert("文档名称不可为空，重新输入")
            return inpDocumentName()
        }
    }
    /**
     * 获取地址栏信息
     * @param name 参数名称
     * @returns {string| 参数值}
     */
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]); return null; //返回参数值
    }
    var config = {}
    var docEditor;
    var innerAlert = function (message) {
        if (console && console.log) console.log(message);
    };
    var onAppReady = function () {
        innerAlert("Document editor ready");
    };
    var onDocumentStateChange = function (event) {
        var title = document.title.replace(/\*$/g, "");
        document.title = title + (event.data ? "*" : "");
    };
    var onRequestEditRights = function () {
        location.href = location.href.replace(RegExp("mode=view&?", "i"), "");
    };
    var onError = function (event) {
        if (event) innerAlert(event.data);
    };
    var onOutdatedVersion = function (event) {
        location.reload(true);
    };
    var replaceActionLink = function (href, linkParam) {
        var link;
        var actionIndex = href.indexOf("&actionLink=");
        alert(actionIndex);
        if (actionIndex != -1) {
            var endIndex = href.indexOf("&", actionIndex + "&actionLink=".length);
            if (endIndex != -1) {
                link =
                    href.substring(0, actionIndex) +
                    href.substring(endIndex) +
                    "&actionLink=" +
                    encodeURIComponent(linkParam);
            } else {
                link =
                    href.substring(0, actionIndex) +
                    "&actionLink=" +
                    encodeURIComponent(linkParam);
            }
        } else {
            alert(linkParam);
            link = href + "&actionLink=" + encodeURIComponent(linkParam);
        }
        alert(href);
        alert(link);
        return link;
    };
    var onMakeActionLink = function (event) {
        alert();
        var actionData = event.data;
        alert(actionData);
        var linkParam = JSON.stringify(actionData);
        alert(linkParam);
        docEditor.setActionLink(replaceActionLink(location.href, linkParam));
    };
    var onRequestSendNotify = function (event) {
        var ACTION_DATA = event.data.actionLink;
        var comment = event.data.message;
        var emails = event.data.emails;
    };
    var onCollaborativeChanges = function () {
        alert("bierengaine");
    };
    var onDocumentReady = function () { };
    var onRequestUsers = function () {
        docEditor.setUsers({
            users: [
                {
                    email: "john@example.com",
                    name: "John Smith",
                },
                {
                    email: "kate@example.com",
                    name: "Kate Cage",
                },
            ],
        });
    };
    var config = this.config;
    config.width = "100%";
    config.height = "100%";
    config.events = {
        onAppReady: onAppReady,
        onDocumentStateChange: onDocumentStateChange,
        onRequestEditRights: onRequestEditRights,
        onError: onError,
        onOutdatedVersion: onOutdatedVersion,
        onMakeActionLink: onMakeActionLink,
        onRequestSendNotify: onRequestSendNotify,
        onCollaborativeChanges: onCollaborativeChanges,
        onDocumentReady: onDocumentReady,
        onRequestUsers: onRequestUsers,
    };
    var сonnectEditor = function () {
        docEditor = new DocsAPI.DocEditor("iframeEditor", config);
    };
    if (window.addEventListener) {
        // window.addEventListener("load", сonnectEditor);
    } else if (window.attachEvent) {
        // window.attachEvent("load", сonnectEditor);
    }
</script>